﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Locator.Silverlight.Client.Models.ProductsServiceReference;
using System.Collections.Generic;
using Locator.Silverlight.Client.Models.ViewModels.Elements;
using System.Linq;

namespace Locator.Silverlight.Client.Models
{
    public class ProductModel : ModelBase<IProductsServiceAsync>, IProductModel
    {
        /// <summary>
        /// Initializes a new instance of the ProductModel class.
        /// </summary>
        public ProductModel()
            : base(new ProductsServiceClient("BasicHttpBinding_IProductsService"))
        {

        }

        public ProductModel(IProductsServiceAsync service)
            : base(service)
        {

        }

        public void GetAllSubstancesAsync(Action<IEnumerable<SubstanceViewModel>, Exception> callback)
        {
            this.Service.GetAllSubstancesCompleted += (sender, eventArgs) =>
            {
                if (eventArgs.Error == null)
                {
                    var substanceViewModelList = new List<SubstanceViewModel>();
                    foreach (var substance in eventArgs.Result)
                    {
                        substanceViewModelList.Add(new SubstanceViewModel(substance));
                    }

                    callback(substanceViewModelList, null);
                }
                else
                {
                    callback(null, eventArgs.Error);
                }
            };

            this.Service.GetAllSubstancesAsync();
        }

        public void GetProductsForUserAsync(Action<IEnumerable<UserProductViewModel>, Exception> callback)
        {
            this.Service.GetProductsForCurrentUserCompleted += (sender, eventArgs) =>
            {
                if (eventArgs.Error == null)
                {
                    var userProductViewModelList = new List<UserProductViewModel>();
                    foreach (var product in eventArgs.Result)
                    {
                        userProductViewModelList.Add(new UserProductViewModel(product));
                    }

                    callback(userProductViewModelList, null);
                }
                else
                {
                    callback(null, eventArgs.Error);
                }
            };

            this.Service.GetProductsForCurrentUserAsync();
        }

        public void GetAllProductsAsync(Action<IEnumerable<ProductViewModel>, Exception> callback)
        {
            this.Service.GetAllProductsCompleted += (sender, eventArgs) =>
            {
                if (eventArgs.Error == null)
                {
                    var productViewModelList = new List<ProductViewModel>();
                    foreach (var product in eventArgs.Result)
                    {
                        productViewModelList.Add(new ProductViewModel() { Id = product.Id, Name = product.Name });
                    }

                    callback(productViewModelList, null);
                }
                else
                {
                    callback(null, eventArgs.Error);
                }
            };

            this.Service.GetAllProductsAsync();
        }

        public void GetAllProductsContaingNameAync(string name, Action<IEnumerable<UserProductViewModel>, Exception> callback)
        {
            this.Service.FindProductsContainingNameCompleted += (sender, eventArgs) =>
            {
                if (eventArgs.Error == null)
                {
                    var userProductViewModelList = new List<UserProductViewModel>();
                    foreach (var userProduct in eventArgs.Result)
                    {
                        userProductViewModelList.Add(new UserProductViewModel(userProduct));
                    }

                    callback(userProductViewModelList, null);
                }
                else
                {
                    callback(null, eventArgs.Error);
                }
            };

            this.Service.FindProductsContainingNameAsync(name);
        }

        public void AddProductForUserAsync(UserProductViewModel product, Action<Exception> callback)
        {
            this.Service.AddProductForCurrentUserCompleted += (sender, eventArgs) =>
            {
                callback(eventArgs.Error);
            };

            this.Service.AddProductForCurrentUserAsync(product.Id);
        }

        public void DeleteProductForUserAsync(ProductViewModel product, Action<Exception> callback)
        {
            this.Service.DeleteProductForCurrentUserCompleted += (sender, eventArgs) =>
            {
                callback(eventArgs.Error);
            };

            this.Service.DeleteProductForCurrentUserAsync(product.Id);
        }
    }
}
